package com.cy.controller;


import com.cy.pojo.Equipment;
import com.cy.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cy
 * @since 2021-11-21
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController {
    
    @Autowired
    private EquipmentService service;

    @ResponseBody
    @RequestMapping("/queryPage")
    public Map<String, Object> query(@RequestBody @Nullable Map<String, Object> mymap) {
//        Integer pageNumber = null;
//        if (!mymap.containsKey("pagenumber")) {
//            pageNumber = 1;
//        }else {
//            pageNumber = (Integer) mymap.get("pagenumber");
//        }
        HashMap<String, Object> map = new HashMap<>();
        List<Equipment> list = service.queryPage(1);
        map.put("equipmentlist", list);
//        map.put("pagenustmber",pageNumber);
        return map;
    }



    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, Object> insert(@RequestBody Equipment equipment) {
        System.out.println(equipment);
        HashMap<String, Object> map = new HashMap<>();
        if (service.save(equipment)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/disable")
    public Map<String, Object> disable(@RequestBody Map<String, Object> mymap) {
        HashMap<String, Object> map = new HashMap<>();
        Integer id = (Integer) mymap.get("id");
        Equipment equipment = new Equipment(id,0);
        if (service.updateById(equipment)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/dimission")
    public Map<String, Object> dimission(@RequestBody Map<String, Object> mymap) {
        HashMap<String, Object> map = new HashMap<>();
        Integer id = (Integer) mymap.get("id");
        Equipment equipment = new Equipment(id,2);
        if (service.updateById(equipment)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/update")
    public Map<String, Object> update(@RequestBody Equipment equipment) {
        HashMap<String, Object> map = new HashMap<>();
        if (service.updateById(equipment)) {
            map.put("message", true);
        } else {
            map.put("message", false);
        }
        return map;
    }


    @ResponseBody
    @PostMapping("/queryName")
    public Map<String, Object> queryByName(@RequestBody Map<String, Object> mymap) {
        String name = (String) mymap.get("name");
        Integer pageNumber = (Integer) mymap.get("pagenumber");
        if (pageNumber == null) {
            pageNumber = 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        List<Equipment> list = service.queryPage(pageNumber,name);
        map.put("equipmentlist", list);
        map.put("pagenumber",pageNumber);
        return map;
    }

}

